package com.neiquan.neiquandemo.activity;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neiquan.neiquandemo.R;
import com.neiquan.neiquandemo.test.FourFragment;
import com.neiquan.neiquandemo.test.OneFragment;
import com.neiquan.neiquandemo.test.SiderBarFragment;
import com.neiquan.neiquandemo.test.ThreeFragment;
import com.neiquan.neiquandemo.test.TwoFragment;
import com.neiquan.neiquandemo.utils.BottomLayout;

import net.neiquan.applibrary.barlibrary.ImmersionBar;
import net.neiquan.applibrary.base.BaseActivity;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者 ： dyj
 * 时间 ： 2016/12/29.
 */

public class MainActivity extends BaseActivity {

    @InjectView(R.id.mBottomView)
    BottomLayout mBottomView;
    private int imgsOn[] = {R.mipmap.tab_1_h, R.mipmap.tab_2_h, R.mipmap.tab_3_h, R.mipmap.tab_4_h};
    private int imgsOff[] = {R.mipmap.tab_1_n, R.mipmap.tab_2_n, R.mipmap.tab_3_n, R.mipmap.tab_4_n};
    private String names[] = {"首页", "分类", "购物车", "我的"};
    private Fragment fragments[] = {new OneFragment(), new SiderBarFragment(), new ThreeFragment(), new FourFragment()};
    private FragmentTransaction fragmentTransaction;

    @Override
    public int getRootViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void setViews() {
        setDefaultFragment(true, 0);
        initBottomMenu();
    }

    @Override
    public void initData() {

    }

    private void initBottomMenu() {
//        String url = "";
//        String subPath = "";
//        for (int j = 0; j < themesize; j++) {
//            tabBean.subjectsBean bean = tab.getSubjects().get(j);
//            if (bean.getType() == theme) {
//                url = bean.getSubjectImgUrl();
//                subPath = bean.getLocalFilePath();
//                break;
//            }
//        }
//        File myCaptureFile = new File(tab.getLocalFilePath());
//        File themeNameFile = new File(subPath);
//        if (myCaptureFile.exists() && themeNameFile.exists()) {
//            bottom_menu.addTab(new BottomLayout.Tab(tab.getName(), 0, tab.getLocalFilePath(), subPath, R.color.bottomBarHint, currThemeColor, tab.getCsstype(), tab.getHideText()));
//        } else {
//            bottom_menu.addTab(new BottomLayout.Tab(tab.getName(), 1, tab.getImgUrl(),
//                    url,
//                    R.color.bottomBarHint,
//                    currThemeColor, tab.getCsstype(), tab.getHideText()));
//        }
        for (int i = 0; i < fragments.length; i++) {
            mBottomView.addTab(new BottomLayout.Tab(names[i], imgsOff[i], imgsOn[i]));
        }
        mBottomView.getTabView(0).setSelected();
        mBottomView.setOnTabSelectListener(new BottomLayout.OnTabSelectListener() {
            @Override
            public void onTabSelected(BottomLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        mImmersionBar.fitsSystemWindows(false).transparentStatusBar().init();
                        break;
                    case 1:
                        mImmersionBar.fitsSystemWindows(true).statusBarColor(R.color.colorAccent).init();
                        break;
                    case 2:
                        mImmersionBar.fitsSystemWindows(false).statusBarColor(R.color.color_blue).init();
                        break;
                    case 3:
                        mImmersionBar.fitsSystemWindows(true).statusBarColor(R.color.green).init();
                        break;
                }
                setDefaultFragment(false, position);
            }
        });
    }

    private void setDefaultFragment(boolean first, int index) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (first) {
            fragmentTransaction.add(R.id.framLayoutId, fragments[0]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[1]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[2]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[3]);
        }
        fragmentTransaction.hide(fragments[0]);
        fragmentTransaction.hide(fragments[1]);
        fragmentTransaction.hide(fragments[2]);
        fragmentTransaction.hide(fragments[3]);
        fragmentTransaction.show(fragments[index]);
        fragmentTransaction.commit();
    }

}
